package com.zero.youxiao.dao;

import java.math.BigInteger;
import java.util.List;

import javax.inject.Inject;

import org.springframework.stereotype.Repository;

import com.zero.basic.dao.DefaultBaseDao;
import com.zero.basic.dao.SqlListBaseDao;
import com.zero.basic.dao.SqlQueryBaseDao;
import com.zero.basic.dao.SqlUpdateBaseDao;
import com.zero.basic.exception.SmsDaoException;
import com.zero.mms.model.SalaryItem;

@Repository("salaryItemDao")
public class SalaryItemDaoImpl extends DefaultBaseDao<SalaryItem> implements
		SalaryItemDao {

	@Inject
	private SqlListBaseDao<?> sqlListBaseDao;

	@Inject
	private SqlQueryBaseDao<?> sqlQueryBaseDao;
	
	@Inject
	private SqlUpdateBaseDao<?> sqlUpdateBaseDao;

	@Override
	public List<SalaryItem> getSalaryItems(String itemName, String itemType)
			throws SmsDaoException {
		try {
			String sql = "select i.* from t_salary_item i";
			boolean hasWhere = false;
			if (!"".equals(itemName)) {
				hasWhere = true;
				sql += " where i.salary_item_name like '%" + itemName+"%'";
			} else if (!"".equals(itemType)) {
				if (hasWhere)
					sql += " and i.salary_item_type = '" + itemType+"'";
				else {
					sql += " where i.salary_item_type = '" + itemType+"'";
				}
			}
			List<SalaryItem> salaryItems = sqlListBaseDao.listBySql(sql, null,
					SalaryItem.class, true, true);
			return salaryItems;
		} catch (Exception e) {
			throw new SmsDaoException();
		}
	}

	@Override
	public BigInteger getOrderList() throws SmsDaoException {
		try {
			String sql = "select count(*) from t_salary_item i where i.salary_show = '是'";
			return (BigInteger) sqlQueryBaseDao.queryBySql(sql, null, null, false, false);
		} catch (Exception e) {
			e.printStackTrace();
			throw new SmsDaoException();
		}
	}

	@Override
	public List<SalaryItem> getAllParItems() throws SmsDaoException {
		try {
			String sql = "select i.* from t_salary_item i where i.salary_item_type = '固定项目' or i.salary_item_type = '导入项目' ";
			List<SalaryItem> salaryItems = sqlListBaseDao.listBySql(sql, null, SalaryItem.class, true, true);
			return salaryItems;
		} catch (Exception e) {
			throw new SmsDaoException();
		}
	}

	@Override
	public void updateOrder(Integer itemOrder, Integer lastOrder)
			throws SmsDaoException {
		try {
			String sql = "update t_salary_item i set i.salary_order = ? where i.salary_order = ?";
			sqlUpdateBaseDao.updateBySql(sql,new Object[]{lastOrder,itemOrder});
		} catch (Exception e) {
			throw new SmsDaoException();
		}
	}

}
